<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>login</title>
</head>
<body>
    <div>
        <h1>登录界面</h1>
        <div>
          用户名：<input type="text" id="username">
        </div>
        <div>
          密码：<input type="password" id="password">
        </div>
        <div>
          <button id="login">登录</button>
        </div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.js"></script>
    <script>
        var login = document.querySelector('#login');
        var username = document.querySelector('#username');
        var password = document.querySelector('#password');

        login.onclick = () => {
          fetch('/api/login', {
            method: 'POST',
            body: JSON.stringify({
              username: username.value,
              password: password.value,
            }),
            headers: {
              'Content-Type': "application/json"
            }
          }).then(res => res.json()).then(res => {
            console.log(res)
            if(res.status === 'success' && res.data) {
                window.location.href = '/'
            } else {
                alert('账号密码不匹配')
            }
          })
        }
    </script>
</body>
</html>